package com.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mall.mapper.CategoryMapper;
import com.mall.mapper.WaresMapper;
import com.mall.pojo.Category;
import com.mall.pojo.Wares;
import com.mall.service.ICategoryService;
import com.mall.service.IWaresService;
import com.mall.vo.SearchCategoryVo;
import com.mall.vo.WareVo;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author xiao-chen
 * @since 2023-04-20
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService {
    @Resource
    private WaresMapper waresMapper;
    @Resource
    private CategoryMapper categoryMapper;
    @Resource
    private IWaresService waresService;


    @Override
    public List<SearchCategoryVo> listCategoryWithTree() {
        //赋值时忽略的数据
        String[] ignore = {"createDate", "updateDate", "updateUser", "createUser"};
        List<Category> categoryList = categoryMapper.selectList(null);
        return categoryList.stream().filter(item -> item.getParentId() == 0).map(item -> {
            SearchCategoryVo categoryVo = new SearchCategoryVo();
            BeanUtils.copyProperties(item, categoryVo, ignore);
            //递归调用获取孩子
            categoryVo.setChildren(getChildren(item, categoryList));
            //获取三级分类下的商品集合
            List<WareVo> collect = waresService.getWaresByCategoryId(item.getId()).stream().map(ware -> {
                WareVo wareVo = new WareVo();
                BeanUtils.copyProperties(ware, wareVo);
                return wareVo;
            }).collect(Collectors.toList());
            categoryVo.setWares(collect);
            return categoryVo;
        }).toList();
    }

    @Override
    public String getCategoryNameByWareId(Long wareId) {
        return categoryMapper.getCategoryNameByWareId(wareId);
    }

    @Override
    public Long getCategoryIdByName(String name) {
        return categoryMapper.getCategoryIdByName(name);
    }

    public List<SearchCategoryVo> getChildren(Category category, List<Category> all) {
        String[] ignore = {"createDate", "updateDate", "updateUser", "createUser"};
        return all.stream().filter(item ->
                item.getParentId().equals(category.getId())
        ).map(item -> {
            SearchCategoryVo categoryVo = new SearchCategoryVo();
            BeanUtils.copyProperties(item, categoryVo, ignore);
            categoryVo.setChildren(getChildren(item, all));
            //获取三级分类下的商品集合
            List<WareVo> collect = waresService.getWaresByCategoryId(item.getId()).stream().map(ware -> {
                WareVo wareVo = new WareVo();
                BeanUtils.copyProperties(ware, wareVo);
                return wareVo;
            }).collect(Collectors.toList());
            categoryVo.setWares(collect);
            return categoryVo;
        }).toList();


    }

    @Override
    public Page<Wares> getWaresByCategory(String categoryId, Integer page, Integer pageSize) {
        LambdaQueryWrapper<Wares> wrapper = new LambdaQueryWrapper<>();
        Page<Wares> warePage = new Page<>(page, pageSize);
        wrapper.eq(Wares::getCategoryId, categoryId);
        waresService.page(warePage, wrapper);

        return warePage;

    }
}
